<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/20 0020
 * Time: 上午 9:15
 */

namespace app\index\controller;


use app\common\Auth;
use think\Controller;
use think\Request;

class User extends Controller
{
    public function login( )
    {
        $referer = input('referer')?input('referer'):'/';
        Auth::logout();
        $user_name = input('user_name');
        $password = input('password');
        if($user_name && $password){
            $user = \app\common\model\User::get([
                "user_name"=>$user_name,
                "password"=>Auth::pwd($password)
            ]);
            if(!empty($user)){
                Auth::setUser($user);
                return redirect($referer);
            }else{
                return $this->error("密码错误");
            }
        }
        return view("admin/user/login");
    }
    public function register()
    {
        $mobile = input('mobile');
        $user_name = input('user_name');
        $password = input('password');
        if($user_name && $password && $mobile){
            if(\app\common\model\User::is_exist($user_name,$mobile)){
                return $this->error("用户已存在");
            }else{
                $user = \app\common\model\User::addUser($user_name,$mobile,Auth::pwd($password));
                Auth::setUser($user);
                return redirect('/');
            }
        }
        return view("admin/user/register");
    }
}